*-------------------------------------------------------------------------------
* TITLE: 4.appendix_tables.do
* DESCRIPTION: Code to create tables for online appendix
* VERSION: MP17.0
* DATE: 08/26/21 
*-------------------------------------------------------------------------------

	/* NOTE:
		The stata command lpoly for local polynomial regression changed between 
		stata versions 15 and 16. The  optimal estimation bandwidth computed by 
		default seems to be smaller in stata 16. For consistency with earlier 
		versions of the paper and across stata versions, we set the bandwidth 
		manually such that resuls are identical to the default output of stata 15,
		whenever we use the lpoly command. The main results of the paper change 
		only marginally when the default of stata 16 or later is used instead. 
	*/
	
**------------------------------------------------------------------------------
** SETTINGS
**------------------------------------------------------------------------------
global Out "${Output}/appendix"


* figure output specifications
local fig_w = 5
local fig_res = `fig_w' * 600 
local fig_type tif
local figure_spec as(`fig_type') width(`fig_res')
local fig_ext ".`fig_type'"

*Adjusting graphical parameters
grstyle init
grstyle set plain, horizontal 

**------------------------------------------------------------------------------
** FIGURE B1: Positive Asset Shocks for Control Households
**------------------------------------------------------------------------------

	* define sample
use "$Data/PovertyTraps_analysis.dta", clear
keep if survey==1 & stup==1 & treat==0 

	* compute share of Households with shock>X
count if deltaLk2!=.
local num_obs_d2 = r(N)
count if deltaLk3!=.
local num_obs_d3 = r(N)

gen shock_size = .
gen share_d2 = .
gen share_d3 = .

local i = 0   
forvalues w=0(0.1)4 {
	local i = `i'+1
	replace shock_size = `w' in `i'
	count if deltaLk2>`w' & deltaLk2!=.
	replace share_d2 = r(N) / `num_obs_d2' in `i'
	count if deltaLk3>`w'  & deltaLk2!=.
	replace share_d3 = r(N) / `num_obs_d3' in `i'
}

	* create figure
tw 	(connect share_d2 shock_size, msize(small) msymbol(O) mcolor(black) lcolor(black)) ///
	(connect share_d3 shock_size, msize(small) msymbol(T) mcolor(gs10) lcolor(gs10) ) ///
	, xline(2.333, lpattern(dash))  ///
	xtitle(change in log productive assets) ytitle(share of control households) ///
	legend(label(1 "2007-2009") label(2 "2007-2011")) 
graph export "${Out}/control_shocks_d2+d3`fig_ext'", replace `figure_spec'



**------------------------------------------------------------------------------
** FIGURE B2: Alternative Estimates of the Transition Equation
**------------------------------------------------------------------------------

* FIGURE B2 a: 3rd Order Polynomial --------------------------------------------

	* define sample
use "$Data/PovertyTraps_analysis.dta", clear
keep if survey==1 & stup==1 & treat==1 & Lk1<=3

forval i=2/3 {
	gen Lk1_`i'=Lk1^`i'
}

	* create figure
reg Lk3 Lk1 Lk1_2 Lk1_3 
predict polynom_deg_3
qui sum Lk1
tw 	(scatter polynom_deg_3 Lk1, connect(l i) msymbol(i O) sort lcolor(black)) ///
	(line Lk1 Lk1, sort lpattern(dash) lcolor(gs10)) ///
	, xtitle("baseline productive assets post-transfer (2007)") ytitle("productive assets in 2011") ///
	legend(off) 
graph export "${Out}/polynomial_deg3_treatedUP`fig_ext'", replace `figure_spec'


* Find Poverty Threshold using FIGURE B2 a -------------------------------------

	* define program to find threshold
cap program drop findthrpoly
program define findthrpoly, rclass
	syntax [if] [in]
	
	quietly: reg Lk3 Lk1 Lk1_2 Lk1_3 `if' `in'
	
	local a = _b[_cons]
	local b = _b[Lk1] - 1
	local c = _b[Lk1_2]
	local d = _b[Lk1_3]
	
	mata thrpoly(`a', `b', `c', `d')
	return add
end

mata
	void thrpoly(real scalar a, real scalar b, real scalar c, real scalar d)
	{
		ss = polyroots((a, b, c, d))
		thr = Re(ss[2])
		st_numscalar("r(thr)", thr)
	}
end

	* define sample
use "$Data/PovertyTraps_analysis.dta", clear
keep if survey==1 & stup==1 & treat==1 & Lk1<=3

forval i=2/3 {
	gen Lk1_`i'=Lk1^`i'
}

	* compute threshold 
findthrpoly
disp "Threshold in log 1000BDT = " r(thr)
disp "Threshold in BDT = " (exp(r(thr))-1)*1000									
disp "Threshold in USD PPP = " = (exp(r(thr))-1)*1000 / 18.46

* compute standard error: 
bootstrap r(thr), reps(1000): findthrpoly


**------------------------------------------------------------------------------
** FIGURE B3: What explains the Bimodal Distribution of Assets?
**------------------------------------------------------------------------------

* FIGURE B3 a: Distribution of Savings Rate ------------------------------------

	* define sample
use "$Data/PovertyTraps_analysis.dta", clear
keep if treat==1

	* compute savings rate net of survey wave and branch FE
gen savRate=savings/(savings+(pce_total*hhsize_adult_eq))

xtset hhid survey_wave
xtreg savRate i.survey i.branchid, fe
predict sfe, u

	*create figure
tw (kdensity sfe [aweight = popWeight] ,  xtitle(" saving rate") ytitle("density"))
graph export "${Out}\kdensity_sRate`fig_ext'", replace `figure_spec'


* FIGURE B3 b: Distribution of Productivity, panel regression ------------------

	* define sample
use "$Data/PovertyTraps_analysis.dta", clear
keep if treat==1

	* compute livestock productivity as residual TFP
for var livestock_inc_tot livestock_hours_tot cows_no: gen LNX=ln(X)

xtset hhid survey_wave
xtreg LNlivestock_inc_tot LNlivestock_hours_tot LNcows_no i.survey i.branchid, fe
predict A, u
replace A = exp(A)

	* create figure
tw 	(kdensity A [aweight = popWeight] if A<50) ///
	, xtitle("individual productivity (A)") ytitle("density")
graph export "${Out}\kdensity_TFP`fig_ext'", replace `figure_spec'


**------------------------------------------------------------------------------
** FIGURE B4: Local polynomial regressions of baseline characteristics 
**	on productive assets
**------------------------------------------------------------------------------

* FIGURE B4 a: Nutrition and Liquidity -----------------------------------------

	* define sample
use "$Data/PovertyTraps_analysis.dta", clear
keep if survey==1 & stup==1 & treat==1 & Lk1<=3

	* prepare variables
gen saving=ln((savings/1000)+1)
replace loan_total_value =ln((loan_total_value /1000)+1)
gen calories=ln(calorie_percap)
replace bmi = . if bmi>50
replace bmi = ln(bmi)
gen HHconsumption = ln(pce_total * hhsize_adult_eq +1)

gen aboveT=Lk1 > 2.333 if Lk1!=.

lab var HHconsumption "log HH consumption"
lab var calories "log calories"
lab var bmi "log BMI"
lab var loan_total_value "log loan value"
lab var saving "log savings"

	* create figure
local vars calories bmi HHconsumption loan_total_value saving 
local cnt = 0
local gcomb
foreach v of local vars {
	local cnt = `cnt' + 1
	local title: variable label `v'
	tw 	(lpolyci `v' Lk1 if aboveT==0) ///
		(lpolyci `v' Lk1 if aboveT==1) ///
		, graphregion(color(white)) title("`title'") name(g`cnt', replace) ///
		xtitle(productive assets) ///
		legend(off) //
	local gcomb `gcomb' g`cnt'
}
graph combine `gcomb' , rows(2)
graph export "${Out}/lpoly_bl_nutrition`fig_ext'", replace `figure_spec'


* FIGURE B4 b: asset composition -----------------------------------------------


	* define sample
use "$Data/PovertyTraps_analysis.dta", clear
keep if survey==1 & stup==1 & treat==1 & Lk1<=3

	* prepare variables
rename asset_value8 shed_value
gen vehicle_value = asset_value12 + asset_value13 
for var poultry_value goat_value shed_value vehicle_value cows_value: qui replace X=ln((X/1000)+1)

gen aboveT=Lk1 > 2.333 if Lk1!=.

lab var poultry_value "log poultry value"
lab var goat_value "log goat value"
lab var shed_value "log shed value"
lab var vehicle_value "log vehicle value"
lab var cows_value "log cows value"


*lpoly graphs: Asset composition
local vars poultry_value goat_value shed_value vehicle_value cows_value
local cnt = 5
local gcomb
foreach v of local vars {
	local cnt = `cnt' + 1
	local title: variable label `v'
	di "`title'"
	tw 	(lpolyci `v' Lk1 if aboveT==0) ///
		(lpolyci `v' Lk1 if aboveT==1) ///
		, graphregion(color(white)) title("`title'") name(g`cnt', replace) ///
		xtitle(productive assets) ///
		legend(off) 
	local gcomb `gcomb' g`cnt'
}
graph combine `gcomb' , rows(2)
graph export "${Out}/lpoly_bl_assetComp`fig_ext'", replace `figure_spec'


**------------------------------------------------------------------------------
** FIGURE B6: Asset accumulation above and below khat
**------------------------------------------------------------------------------

	* define sample
use "$Data/PovertyTraps_analysis.dta", clear
keep if stup==1 & survey==1 & treat==1 & Lk1<=3
local khat = 2.34
gen aboveT=Lk1>=`khat'

local thisbw_b = 0.05
local thisbw_a = 0.07

tw	(lpolyci deltaLk3 Lk1 if aboveT==0, bw(`thisbw_b') fcolor(gray%20))  ///
	(lpolyci deltaLk3 Lk1 if aboveT==1, bw(`thisbw_a') fcolor(gray%20)) ///
	, xtitle("baseline assets plus transfer") ///
	ytitle("change in productive assets 2007-11")  legend(off)
graph export "${Out}/lpoly_deltaLk3`fig_ext'", replace `figure_spec'


**------------------------------------------------------------------------------
** FIGURE B7: Transition Equation with Human Capital Controls
**------------------------------------------------------------------------------

	* define sample
use "$Data/PovertyTraps_analysis.dta", clear
keep if survey==1&stup==1&Lk1<=3 & treat==1

	* prepare variables: generate residuals
replace bmi = . if bmi>50	
gen bmi_sqr = bmi^2
gen age_sqr = resp_age^2
gen resp_educ_sqr = resp_educ_years^2
egen sum_H_activity = rowtotal(H_activity*)

global controls resp_age age_sqr resp_educ_years resp_educ_sqr resp_literate resp_numerate bmi bmi_sqr sum_H_activity happy H_anxietyD 
reg Lk1 $controls
predict res_Lk1, residuals

reg Lk3 $controls
predict res_Lk3, residuals


tw 	(lpolyci  res_Lk3 res_Lk1) ///
	(line res_Lk1 res_Lk1, lpattern(dash) lcolor(gs10) sort) ///
	, legend(off) ///
	xtitle("baseline (2007) productive assets plus transfer" "(residualized)") ///
	ytitle("productive assets in 2011" "(residualized)" " ") 
graph export "${Out}/lpoly_Lk1_Lk3_withControls`fig_ext'", replace `figure_spec'



**------------------------------------------------------------------------------
** FIGURE B8: Asset Stock over the Life-Cycle: Control Villages
**------------------------------------------------------------------------------

	* define sample
use "$Data/PovertyTraps_analysis.dta", clear
keep if treat==0 & stup==0 & resp_age<80 


gen lA=log(pAssets+1)

local thisbw = 2.8

tw 	(lpoly lA resp_age if survey_wave==1, bw(`thisbw') lcolor(navy%30)) ///
	(lpoly lA resp_age if survey_wave==2, bw(`thisbw') lcolor(navy%50)) ///
	(lpoly lA resp_age if survey_wave==3, bw(`thisbw') lcolor(navy%80)) ///
	(lpoly lA resp_age if survey_wave==4, bw(`thisbw') lcolor(navy) ) ///
	, xtitle(age) ytitle(log(productive assets)) ///
	legend(label(1 "2007") label(2 "2009") label(3 "2011") label(4 "2014")) ///
	xticks(20(10)80)
graph export "${Out}/LifeCycle_Control_OtherMiddle`fig_ext'", replace `figure_spec'



**------------------------------------------------------------------------------
** FIGURE B9: Productive Asset Dynamics in the Long Run above and below the 
*	Poverty Threshold by Age
**------------------------------------------------------------------------------


	* define sample
use "$Data/PovertyTraps_analysis.dta", clear
keep if stup==1 & treat==1 & Lk1<=3 

gen aboveT = Lk1 >= 2.333

	* label variables
label define abovelab 0 "below" 1 "above"
label val aboveT abovelab

local slab_above
local slab_below
local cnt =0
foreach s in 2007 2009 2011 2014 2018 {
	local cnt = `cnt' +1
	 su resp_age if survey==`cnt' , d
	local p25 = r(p25)
	local p75 = r(p75)
	local slab_below `slab_below' `cnt' `" "`s'" "[`p25']" "'
	local slab_above `slab_above' `cnt' `" "`s'" "[`p75']" "'
}

label define slab_above `slab_above'
label define slab_below `slab_below'

	* compute average transfer value
qui su Pcows_no if survey==1, d
local Tvalue = r(mean) / 1000


	* split at median age
su resp_age if survey==1, d
gen above_med_age = age1 > r(p50) & age1!=.

	* collapse data to percentiles
local plist 10 20 30 40 50 60 70 80 90 
local collapse_pA
foreach p of local plist {
	local collapse_pA `collapse_pA' (p`p') pAssets_p`p' = pAssets
}
collapse `collapse_pA' , by(survey aboveT above_med_age)

		* below median baseline age
label value survey slab_below

tw line pAssets_p* survey if aboveT ==0 & above_med_age==0 ///
	, lcolor(gs13 gs11 gs8 gs7 gs6 gs5 gs4 gs3 gs1) ///
	lpattern( shortdash_dot dash_dot dash longdash solid longdash dash dash_dot shortdash_dot  ) ///
	yline(`Tvalue')   ///
	xlabel(, valuelabel) ///
	legend(cols(3)) legend(label(1 "p 10") label (2 "p 20") label(3 "p 30") label(4 "p 40") label(5 "p 50") label(6 "p 60") label(7 "p 70") label(8 "p 80") label(9 "p 90")) ///
	yscale(range(0 57)) ylabels(0 (10) 55) ytick(0 (5) 55 ) ymtick(0 (2.5) 57 ) ///
	xtitle(" " "survey wave [median age]") ytitle("productive Assets (in 1000 BDT)" " ") title(Below T) name(gBelow, replace)
	
tw line pAssets_p* survey if aboveT ==1 & above_med_age==0 ///
	, lcolor(gs13 gs11 gs8 gs7 gs6 gs5 gs4 gs3 gs1) ///
	lpattern( shortdash_dot dash_dot dash longdash solid longdash dash dash_dot shortdash_dot  ) ///
	yline(`Tvalue') ///
	xlabel(, valuelabel)  ///
	yscale(range(0 57)) ylabels(0 (10) 55) ytick(0 (5) 55 ) ymtick(0 (2.5) 57 ) ///
	xtitle(" " "survey wave [median age]") title(Above T) name(gAbove, replace)
	
grc1leg  gBelow gAbove, rows(1) legendfrom(gBelow) graphregion(color(white))
graph export "${Out}/ptiles_overS_byaboveT_pA_belowMage`fig_ext'", replace `figure_spec'

		* above median baseline age
label value survey slab_above

tw line pAssets_p* survey if aboveT ==0 & above_med_age==1 ///
	, lcolor(gs13 gs11 gs8 gs7 gs6 gs5 gs4 gs3 gs1) ///
	lpattern( shortdash_dot dash_dot dash longdash solid longdash dash dash_dot shortdash_dot  ) ///
	xlabel(, valuelabel) ///
	yline(`Tvalue') ///
	legend(cols(3)) legend(label(1 "p 10") label (2 "p 20") label(3 "p 30") label(4 "p 40") label(5 "p 50") label(6 "p 60") label(7 "p 70") label(8 "p 80") label(9 "p 90")) ///
	yscale(range(0 57)) ylabels(0 (10) 55) ytick(0 (5) 55 ) ymtick(0 (2.5) 57 ) ///
	xtitle(" " "survey wave [median age]") ytitle("productive Assets (in 1000 BDT)" " ") title(Below T) name(gBelow, replace)
	
tw line pAssets_p* survey if aboveT ==1 & above_med_age==1 ///
	, lcolor(gs13 gs11 gs8 gs7 gs6 gs5 gs4 gs3 gs1) ///
	lpattern( shortdash_dot dash_dot dash longdash solid longdash dash dash_dot shortdash_dot  ) ///
	xlabel(, valuelabel) ///
	yline(`Tvalue') ///
	yscale(range(0 57)) ylabels(0 (10) 55) ytick(0 (5) 55 ) ymtick(0 (2.5) 57 ) ///
	xtitle(" " "survey wave [median age]") title(Above T) name(gAbove, replace)

grc1leg  gBelow gAbove, rows(1) legendfrom(gBelow) graphregion(color(white))
graph export "${Out}/ptiles_overS_byaboveT_pA_aboveMage`fig_ext'", replace `figure_spec'



**------------------------------------------------------------------------------
** FIGURE B12: Distribution of Productive Assets excluding Land
**------------------------------------------------------------------------------

	* define sample
use "$Data/PovertyTraps_analysis.dta", clear
keep if survey == 1

	* compute axis labels 
local labels  10 100 1000 10000 100000
local xlablist 
foreach y of local labels {
	local thislabloc = ln(`y'*18.46/1000+1)
	local thislab = strofreal(`y', "%9.0fc")
	local xlablist `"`xlablist' `thislabloc' "`thislab'" "'
}

	* prepare variables (excluding land)
gen Lkb0 = ln((cows_value + goat_value + poultry_value + bussAss)/ 1000 + 1) if survey==1

	*create figure 
tw 	(kdensity Lkb0 [weight = popWeight] if treat==1, lcolor (blue) xline(3.8)) ///
	(kdensity Lkb0 [weight = popWeight] if treat==0, lcolor (red) lpattern(dash)) ///
	, xtitle("Baseline productive assets" "(USD PPP, log-scale)") ///
	ytitle("density") xlabel(`xlablist') ///
	legend(label(1 "treatment") label(2 "control"))
graph export "${Out}/kdensity_lnpAssets_baseline_pretreat_USD_noland`fig_ext'", replace `figure_spec'

